/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CalculadoraFrame.java
 *
 * Created on 31/08/2011, 10:23:55 AM
 */
package gui;

import Entidad.Numeros;
import Control.Operaciones;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 *
 * @author pepe
 */
public class CalculadoraFrame extends javax.swing.JFrame {

    /** Creates new form CalculadoraFrame */
    public CalculadoraFrame() {
        initComponents();
        mostrarTF.addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e){
                char caracter = e.getKeyChar();
                if(!((caracter < '0') || (caracter > '9'))){
                    insertarEnTF(Integer.parseInt(String.valueOf(caracter)));
                }
                e.consume();
                if(caracter == '+'){
                    botonSumaJBActionPerformed(null);
                }
            }
            
        });
    }
    
    private Double resultado = 0.0, num1 ;
    private boolean hayOperacion = false,mostrar=false;
    Operaciones operacion = new Operaciones();
    Numeros num3 =new Numeros();
    Numeros num2 =new Numeros();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mostrarTF = new javax.swing.JTextField();
        boton1JB = new javax.swing.JButton();
        boton2JB = new javax.swing.JButton();
        boton3JB = new javax.swing.JButton();
        boton4JB = new javax.swing.JButton();
        boton5JB = new javax.swing.JButton();
        boton6JB = new javax.swing.JButton();
        boton7JB = new javax.swing.JButton();
        boton8JB = new javax.swing.JButton();
        boton9JB = new javax.swing.JButton();
        boton0JB = new javax.swing.JButton();
        botonIgualJB = new javax.swing.JButton();
        botonSumaJB = new javax.swing.JButton();
        botonRestaJB = new javax.swing.JButton();
        botonMultiplicarJB = new javax.swing.JButton();
        botonDividirJB = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        botonCosenoJB = new javax.swing.JButton();
        botonLogJB = new javax.swing.JButton();
        botonSenoJB = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        mostrarTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mostrarTFActionPerformed(evt);
            }
        });

        boton1JB.setText("1");
        boton1JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton1JBActionPerformed(evt);
            }
        });

        boton2JB.setText("2");
        boton2JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton2JBActionPerformed(evt);
            }
        });

        boton3JB.setText("3");
        boton3JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton3JBActionPerformed(evt);
            }
        });

        boton4JB.setText("4");
        boton4JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton4JBActionPerformed(evt);
            }
        });

        boton5JB.setText("5");
        boton5JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton5JBActionPerformed(evt);
            }
        });

        boton6JB.setText("6");
        boton6JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton6JBActionPerformed(evt);
            }
        });

        boton7JB.setText("7");
        boton7JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton7JBActionPerformed(evt);
            }
        });

        boton8JB.setText("8");
        boton8JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton8JBActionPerformed(evt);
            }
        });

        boton9JB.setText("9");
        boton9JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton9JBActionPerformed(evt);
            }
        });

        boton0JB.setText("0");
        boton0JB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton0JBActionPerformed(evt);
            }
        });

        botonIgualJB.setText("=");
        botonIgualJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonIgualJBActionPerformed(evt);
            }
        });

        botonSumaJB.setText("+");
        botonSumaJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSumaJBActionPerformed(evt);
            }
        });

        botonRestaJB.setText("-");
        botonRestaJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRestaJBActionPerformed(evt);
            }
        });

        botonMultiplicarJB.setText("*");
        botonMultiplicarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonMultiplicarJBActionPerformed(evt);
            }
        });

        botonDividirJB.setText("/");
        botonDividirJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDividirJBActionPerformed(evt);
            }
        });

        jButton1.setText("CE");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        botonCosenoJB.setText("cos x");
        botonCosenoJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCosenoJBActionPerformed(evt);
            }
        });

        botonLogJB.setText("log x");
        botonLogJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLogJBActionPerformed(evt);
            }
        });

        botonSenoJB.setText("sin x");
        botonSenoJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSenoJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mostrarTF, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(boton4JB)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(boton5JB)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(boton6JB)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(botonRestaJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(boton0JB)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(botonIgualJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(boton7JB)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(boton8JB)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(boton9JB)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(botonDividirJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(botonMultiplicarJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(botonCosenoJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(botonSenoJB)
                                    .addComponent(botonLogJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(boton1JB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(boton2JB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(boton3JB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonSumaJB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mostrarTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(boton1JB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(boton2JB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(boton3JB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(botonSumaJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(boton4JB)
                            .addComponent(boton5JB)
                            .addComponent(boton6JB)
                            .addComponent(botonRestaJB))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(boton7JB)
                            .addComponent(boton8JB)
                            .addComponent(boton9JB)
                            .addComponent(botonMultiplicarJB))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(boton0JB)
                            .addComponent(botonIgualJB)
                            .addComponent(botonDividirJB)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonSenoJB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCosenoJB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonLogJB)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void boton1JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton1JBActionPerformed
        insertarEnTF(1);
}//GEN-LAST:event_boton1JBActionPerformed

private void boton2JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton2JBActionPerformed
        insertarEnTF(2);        
}//GEN-LAST:event_boton2JBActionPerformed

private void boton3JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton3JBActionPerformed
        insertarEnTF(3);
}//GEN-LAST:event_boton3JBActionPerformed

private void boton4JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton4JBActionPerformed
        insertarEnTF(4);
}//GEN-LAST:event_boton4JBActionPerformed

private void boton5JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton5JBActionPerformed
        insertarEnTF(5);
}//GEN-LAST:event_boton5JBActionPerformed

private void boton6JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton6JBActionPerformed
        insertarEnTF(6);
}//GEN-LAST:event_boton6JBActionPerformed

private void boton7JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton7JBActionPerformed
        insertarEnTF(7);
}//GEN-LAST:event_boton7JBActionPerformed

private void boton8JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton8JBActionPerformed
        insertarEnTF(8);
}//GEN-LAST:event_boton8JBActionPerformed

private void boton9JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton9JBActionPerformed
        insertarEnTF(9);
}//GEN-LAST:event_boton9JBActionPerformed

private void boton0JBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton0JBActionPerformed
        insertarEnTF(0);
}//GEN-LAST:event_boton0JBActionPerformed


private void botonSumaJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSumaJBActionPerformed

    num3.setX(Double.valueOf(mostrarTF.getText()));
    mostrarTF.setText("");
    operacion.setOperacion("suma");

}//GEN-LAST:event_botonSumaJBActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    resultado = 0.0;
    num1 = 0.0;
    hayOperacion = false;
    mostrarTF.setText("");
}//GEN-LAST:event_jButton1ActionPerformed

private void botonRestaJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRestaJBActionPerformed
    
    num3.setX(Double.valueOf(mostrarTF.getText()));    // TODO add your handling code here:
    mostrarTF.setText("");
    operacion.setOperacion("resta");
}//GEN-LAST:event_botonRestaJBActionPerformed

private void mostrarTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mostrarTFActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_mostrarTFActionPerformed

private void botonMultiplicarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonMultiplicarJBActionPerformed
   // TODO add your handling code here:

    num3.setX(Double.valueOf(mostrarTF.getText()));
    mostrarTF.setText("");
    operacion.setOperacion("multiplicacion");
}//GEN-LAST:event_botonMultiplicarJBActionPerformed

private void botonDividirJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDividirJBActionPerformed
    // TODO add your handling code here:

    num3.setX(Double.valueOf(mostrarTF.getText()));
    mostrarTF.setText("");
    operacion.setOperacion("division");
}//GEN-LAST:event_botonDividirJBActionPerformed

private void botonIgualJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonIgualJBActionPerformed
    // TODO add your handling code here:
    int x;
    num2.setX(Double.valueOf(mostrarTF.getText()));
    mostrarTF.setText(String.valueOf(operacion.Operar(operacion.getOperacion(), num3, num2)));

}//GEN-LAST:event_botonIgualJBActionPerformed

private void botonCosenoJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCosenoJBActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonCosenoJBActionPerformed

private void botonSenoJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSenoJBActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonSenoJBActionPerformed

private void botonLogJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLogJBActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonLogJBActionPerformed



    private void insertarEnTF(int num) {
        if(mostrar){
            mostrarTF.setText("");
            mostrar = false;
        }
        if( !(mostrarTF.getText().equals(""))){
            mostrarTF.setText(mostrarTF.getText()+num);
        }else{
            mostrarTF.setText(num+"");
        }
    }
    
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CalculadoraFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CalculadoraFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CalculadoraFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CalculadoraFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        

        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CalculadoraFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton0JB;
    private javax.swing.JButton boton1JB;
    private javax.swing.JButton boton2JB;
    private javax.swing.JButton boton3JB;
    private javax.swing.JButton boton4JB;
    private javax.swing.JButton boton5JB;
    private javax.swing.JButton boton6JB;
    private javax.swing.JButton boton7JB;
    private javax.swing.JButton boton8JB;
    private javax.swing.JButton boton9JB;
    private javax.swing.JButton botonCosenoJB;
    private javax.swing.JButton botonDividirJB;
    private javax.swing.JButton botonIgualJB;
    private javax.swing.JButton botonLogJB;
    private javax.swing.JButton botonMultiplicarJB;
    private javax.swing.JButton botonRestaJB;
    private javax.swing.JButton botonSenoJB;
    private javax.swing.JButton botonSumaJB;
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField mostrarTF;
    // End of variables declaration//GEN-END:variables
}
